package com.siemens.industrial.web.mvc.dao.admin;

import com.siemens.industrial.model.admin.TenantServiceRelation;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by qupen on 2016/9/18.
 */
@Service
public class TenantServiceRelationDao {
    @Resource
    JdbcTemplate jdbcTemplate;

    public static final String getCount = "SELECT COUNT(1) FROM `siemens`.`tenant_service_relation` WHERE `` =?";
    public static final String addRelation = "INSERT INTO `siemens`.`tenant_service_relation` (`tenant_id`,`service_id`,`start_date`,`end_date`,`enabled`) VALUES(?,?,?,?,?)";
    public static final String changeRelation = "UPDATE `siemens`.`tenant_service_relation` SET `enabled`=? WHERE `tenant_id`=? AND `service_id`=?";
    public static final String deleteRelation = "DELETE FROM `siemens`.`tenant_service_relation` WHERE `tenant_id`=? AND `service_id`=?";
    public static final String extendDate = "UPDATE `siemens`.`tenant_service_relation` SET ``='' WHERE `tenant_id`=? AND `service_id`=?";
    public static final String getOneByServiceIdAndTenantId="SELECT * FROM `siemens`.tenant_service_relation WHERE `tenant_id` = ? AND `service_id` = ?";
    public static final String updateRelationEndDate = "UPDATE `siemens`.`tenant_service_relation` SET `end_date`=? WHERE `tenant_id`=? AND `service_id`=?";

    public int save(TenantServiceRelation tsr) {
     return jdbcTemplate.update(addRelation,pss->{
         pss.setString(1,tsr.getTenantId());
         pss.setString(2,tsr.getServiceId());
         pss.setDate(3,new java.sql.Date(tsr.getStartDate().getTime()));
         pss.setDate(4,new java.sql.Date(tsr.getEndDate().getTime()));
         pss.setInt(5,tsr.getEnabled());
      });
    }


    public int update(TenantServiceRelation tsr) {
        return jdbcTemplate.update(changeRelation, pss->{
            pss.setString(2,tsr.getServiceId());
            pss.setDate(3,new java.sql.Date(tsr.getStartDate().getTime()));
            pss.setDate(4,new java.sql.Date(tsr.getEndDate().getTime()));
            pss.setInt(5,tsr.getEnabled());
        });
    }

    public int updateEndDate(TenantServiceRelation tsr) {
        return jdbcTemplate.update(updateRelationEndDate, pss->{
           pss.setDate(1,tsr.getEndDate());
            pss.setString(2,tsr.getTenantId());
            pss.setString(3,tsr.getServiceId());
            System.out.print(pss);
        });
    }

    public int getCount(String tenantId) {
        return jdbcTemplate.queryForObject(getCount, Integer.class);
    }

    @Transactional
    public int deleteRelation(String tenantId, String serviceId) {
        return jdbcTemplate.update(deleteRelation, tenantId, serviceId);

    }

    public TenantServiceRelation getOneByTenantIdAndServiceId(String tenantId,String serviceId){
        try {
            return jdbcTemplate.queryForObject(getOneByServiceIdAndTenantId, new Object[]{tenantId,serviceId}, TenantServiceRelation.getRowMapper());
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
